package com.swfee.qinben;

import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.swfee.qinben.base.AppBaseActivity;
import com.swfee.qinben.dto.rsp.LoginRspDto;
import com.swfee.qinben.httpactions.LoginAction;
import com.swfee.qinben.model.LastLogin;
import com.swfee.qinben.service.LastLoginService;
import com.swfee.qinben.session.UserSession;

public class AppLoginActivity extends AppBaseActivity {

	private LayoutInflater inflater;
	private Button btnLogin;
	private LastLoginService lastLoginService;
	Handler handle = new Handler();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_login);
		initLoginbtn();
		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	
	private void initLoginbtn(){
		btnLogin = (Button) findViewById(R.id.btn_login);
		lastLoginService = new LastLoginService(this);
		final TextView loginName = (TextView) findViewById(R.id.login_name);
		final TextView password = (TextView) findViewById(R.id.password);
		final CheckBox rememberMe = (CheckBox) findViewById(R.id.remember_me);
		final LoginAction loginAction = new LoginAction();
		//得到最后一次登录的用户
		final LastLogin lastLoginUsr =  lastLoginService.getLastLogin();
		if(lastLoginUsr!=null){
			loginName.setText(lastLoginUsr.getLoginName());
			password.setText(lastLoginUsr.getPassword());
		}
//		417415575@qq.com
		showMyToast( "现测试开发可用417415575@qq.com/nicai登录", Toast.LENGTH_SHORT);
		btnLogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showProgressDialog("登录中，请稍候");
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						final LoginRspDto dto = loginAction.httpLogin(loginName.getText().toString(), password.getText().toString());
						if(dto.getResult()==1){	//记录登录用户
							if(rememberMe.isChecked()){
								LastLogin lastLogin = new LastLogin();
								lastLogin.setAppCode(dto.getAppCode());
								lastLogin.setLoginDate(new Date());
								lastLogin.setLoginName(loginName.getText().toString());
								lastLogin.setPassword(password.getText().toString());
								lastLoginService.saveLastLogin(lastLogin);
							}else if(lastLoginUsr!=null){//清除以前的登录
								lastLoginService.deleteById(lastLoginUsr.getLoginName());
							}
						}
						
						handle.post(new Runnable() {
							@Override
							public void run() {
								hideProgressDialog();
								if(dto.getResult()==1){
									UserSession.init(dto);
									showMyToast( "登录成功！您好，"+dto.getName(), Toast.LENGTH_SHORT);
									startActivityBeforeFinish(new Intent(AppLoginActivity.this,QinBenActivity.class));
								}else{
									
									showMyToast("登录失败，确认密码是否正确",Toast.LENGTH_SHORT);
								}
								
							}
						});
						
					}
				}).start();
				
			}
		});
	}
	

}
